#!/usr/bin/sh

# Test for temporal algebra in LatLon location.
rm /tmp/input1_test.txt
rm /tmp/input2_test.txt
rm /tmp/input3_test.txt
rm /tmp/input4_test.txt

# Create random points test maps.
for i in {1..60}
  do
    if [[ "$i" -le 20 ]]; then
      echo testpoint1_$i >> /tmp/input1_test.txt
      v.random --o -z output=testpoint1_$i n=3 seed=$i
    elif [ "$i" -gt 20 ] && [ "$i" -le 40 ]; then
      echo testpoint2_$i >> /tmp/input2_test.txt
      v.random --o -z output=testpoint2_$i n=3 seed=$i
    else
      echo testpoint3_$i >> /tmp/input3_test.txt
      echo testpoint4_$i >> /tmp/input4_test.txt
      v.random --o -z output=testpoint3_$i n=3 seed=$i
      v.random --o -z output=testpoint4_$i n=3 seed=$i  
    fi
  done

# Create STVDS and register test maps.
t.create --o output=test1 type=stvds title="test dataset" descr="test dataset"
t.create --o output=test2 type=stvds title="test dataset" descr="test dataset"
t.create --o output=test3 type=stvds title="test dataset" descr="test dataset"
t.create --o output=test4 type=stvds title="test dataset" descr="test dataset"

t.register -i --o type=vect input=test1 file=/tmp/input1_test.txt increment="1 days" start="2013-01-01"
t.register -i --o type=vect input=test2 file=/tmp/input2_test.txt increment="1 days" start="2013-01-10"
t.register -i --o type=vect input=test3 file=/tmp/input3_test.txt increment="3 days" start="2013-01-01"
t.register -i --o type=vect input=test4 file=/tmp/input4_test.txt increment="3 days" start="2013-01-10"

# Test different options.
python t.select.py --o expression="A = test1 : test2" type="stvds"
python t.select.py --o expression="B = test1 !: test2" type="stvds"
python t.select.py --o expression="C = test2 {equal,:} test1" type="stvds"
python t.select.py --o expression='D = test1 : buff_t(test2, "1 days")' type="stvds"
python t.select.py --o expression='E = test1 {during,:} buff_t(test2, "1 days")' type="stvds"
python t.select.py --o expression='F = tsnap(test1 {during,:} buff_t(test2, "1 days"))' type="stvds"
python t.select.py --o expression='G = if(td(test1) == 1, test2)' type="stvds"
python t.select.py --o expression='H = if(td(test1) == 2, test2)' type="stvds"
python t.select.py --o expression='I = if(start_year() == 2013, test2)' type="stvds"
python t.select.py --o expression='I = if(start_year() == 2013 && start_day() < 20, test2)' type="stvds"
python t.select.py --o expression='I = if(start_year() == 2013 && start_day() < 20 && start_day > 15, test2)' type="stvds"
python t.select.py --o expression='I = if(start_year() == 2013 || start_day() < 20 && start_day() > 15, test2)' type="stvds"
python t.select.py --o expression='I = if(td(test1) == 1 || start_day() < 20 && start_day() > 15, test2)' type="stvds"
python t.select.py --o expression='J = if(td(test1) == 1 || start_day() < 20, test2)' type="stvds"

# Test for nested expressions with conditionals.
python t.select.py --o expression='K = if(td(test1) == 1, if(start_day() > 15, test2))' type="stvds"
t.vect.list input=K column=name,start_time
python t.select.py --o expression='L = if(start_day() > 15, test2, test1)' type="stvds"
t.vect.list input=L column=name,start_time
python t.select.py --o expression='L = if({during}, start_day() > 15, test2, test1)' type="stvds"
t.vect.list input=L column=name,start_time
python t.select.py --o expression='L = if({during}, td(test1) == 1, test2, test1)' type="stvds"
t.vect.list input=L column=name,start_time
python t.select.py --o expression='L = if(test1 {#} test2 == 1, test1)' type="stvds"
python t.select.py --o expression='L = if(test3 {contains,#} test2 > 1, test3)' type="stvds"
t.vect.list input=L column=name,start_time
python t.select.py --o expression='M = if({equal|during}, test3 {contains,#} test2 > 1, test3, test1)' type="stvds"
t.vect.list input=M column=name,start_time
python t.select.py --o expression="C = test1 {during,:} test3" type="stvds"
t.vect.list input=C column=name,start_time,end_time
python t.select.py --o expression="C = test1 {during,:} test4" type="stvds"
t.vect.list input=C column=name,start_time,end_time
